<?php 
class boutique_show extends MX_Controller{
	public $limit=10;
	public $page=0; 
	public function index($id=0){

		$this->load->library('session');
		$filter=array('category_id'=>0,'date'=>'');
		$message='aucun boutique ne correspond à votre recherche';
		$sort=array('company_name'=>'desc','shop_counter'=>'desc');
		$search='';
		if(isset($_POST)){
			
			foreach ($_POST as $key=>$val)
			if($key=='sort')
				foreach ($sort as $key=>$val)
					$sort[$key]=$this->input->post($key);
			if(isset($_POST['search'])){
				$search=$this->input->post('search');
				$extract=explode(' ',$search);
				foreach ($extract as $index=>$valeur)
				if($valeur=='' or strlen($valeur)<=2) unset($extract[$index]);
				$filter['search']=implode(' ',$extract);
			}
			if(isset($_POST['search_category']))
				$filter['category_id']=$this->input->post('search_category');
			if(isset($_POST['search_region']))
				$filter['search_region']=$this->input->post('search_region');
			if(isset($_POST['search_cp']))
				$filter['search_cp']=$this->input->post('search_cp');
			
			$this->session->set_userdata($filter);
			
		}
		if($this->page){
			$filter['search']=$this->session->userdata('search');
			$filter['category_id']=$this->session->userdata('category_id');
			$filter['search_region']=$this->session->userdata('search_region');
			$filter['search_cp']=$this->session->userdata('search_cp');
		}
		
		$boutiques=$this->model('shop_models_admin')->get_list_shop($filter,$sort,$this->page,$this->limit);
		
		$Total=$this->shop_models_admin->total_list_shop($filter,$sort);
		
		$limitPage=($this->limit+$this->page)>$Total?$Total:($this->limit+$this->page);
		if($Total) $message=$this->page." - $limitPage de $Total boutiques";
		$title="Vous déposez annonce ".$this->input->post('tilte');
		$description="Téléphones portables, écrans plats LED et LCD, ordinateurs et jeux vidéo, appareil photos, achetez et vendez d’occasion sur Caverne";
		$keywords = preg_split("/[\s,]+/", $this->input->post('tilte'));
		if(is_array($keywords))$keywords='';
		
		$data=array(
				'title'=>$title,
				'description'=>$description,
				'keywords'=>$keywords,
				'paging'=>self::paging($Total),
				'message'=>$message,
				'boutiques'=>$boutiques
		);
		$setting=$this->load->Module('setting/data');

		$data=$setting->data($data);

		$data['nav_class']['boutique']='active';
		 
		$this->view(
					array('boutique-header',
						'boutique-index',
						'footer'),
				$data); // Display the page
	}

	public function page($page=''){
		if($page=='')$page=0;
		$this->page=$page;
		self::index();
	}
	private function pagingAnnonce($Total=0){

		if($Total>$this->limit){
			$this->load->helper('url');
			$paging='';
			
		
			$this->load->library('pagination');
			$config['base_url'] = '#';
			$config['prefix'] ='" onclick="$(\'#page\').val(';
			$config['suffix'] =');$(\'#annonce_chercher\').submit()';
			
			$config['cur_page'] = $this->page;
			//$config['page_query_string'] = true;
			
			$config['total_rows'] = $Total;
			$config['per_page'] = $this->limit;
			$config['next_tag_open'] =$config['last_tag_open'] =$config['first_tag_open'] =$config['prev_tag_open'] =$config['num_tag_open'] = '<li>';
			$config['next_tag_close'] =$config['last_tag_close']  =$config['first_tag_close'] =$config['prev_tag_close'] =$config['num_tag_close'] = '</li>';
			$config['cur_tag_open'] =' <li class="disabled"><a href="#">';
			$config['cur_tag_close']='</a></li>';
			$this->pagination->initialize($config);
			return $this->pagination->create_links();
		}
		return '';
	}
	private function paging($Total=0){

		if($Total>$this->limit){
			$this->load->helper('url');
			$paging='';
			$current_url=current_url();
			$extract=explode('/page',$current_url);
			if(isset($extract[0]))$current_url=$extract[0];
		
			$this->load->library('pagination');
			$config['base_url'] = $current_url.'/page/';
			$config['cur_page'] = $this->page;
		
			$config['total_rows'] = $Total;
			$config['per_page'] = $this->limit;
			$config['next_tag_open'] =$config['last_tag_open'] =$config['first_tag_open'] =$config['prev_tag_open'] =$config['num_tag_open'] = '<li>';
			$config['next_tag_close'] =$config['last_tag_close']  =$config['first_tag_close'] =$config['prev_tag_close'] =$config['num_tag_close'] = '</li>';
			$config['cur_tag_open'] =' <li class="disabled"><a href="#">';
			$config['cur_tag_close']='</a></li>';
			$this->pagination->initialize($config);
			return $this->pagination->create_links();
		}
		return '';
	}
	public function link($shop_link=''){

		$boutique=false;
		$title=$description=$keywords = '';
		$id=$this->model()->get_id_from_link($shop_link);
		$Total=0;
		$message="la boutique n'a pas posté ";
		$search='';
		if(isset($_POST['search'])){
			$search=$this->input->post('search');
		}
		if(isset($_POST['page'])){
			$this->page=$this->input->post('page');
		}

		$setting=$this->load->Module('setting/data');
		if($id) $boutique=self::get_info($id,true,$search);
		if($boutique){
			$Total=$boutique->nb_annonces;
			$limitPage=($this->limit+$this->page)>$Total?$Total:($this->limit+$this->page);
			if($Total) $message=$this->page." - $limitPage de $Total annonces";
			//si annonce est non valide
			
			$boutique->site_link='';
			if($boutique->site){
				$boutique->site_link=$boutique->site;
				$site=parse_url($boutique->site);
				$scheme=isset($site['scheme'])?$site['scheme']:'http';
				$host=isset($site['host'])?$site['host']:'';
				$user=isset($site['user'])?$site['user']:'';
				$pass=isset($site['pass'])?':'.$site['pass'].'@':'';
				$path=isset($site['path'])? $site['path']:'';
				$query=isset($site['query'])?'?'.$site['query']:'';
				$fragment=isset($site['fragment'])?'#'.$site['fragment']:'';
				
				$boutique->site="<a href='$scheme://$user$pass$host$path$query$fragment'>{$host}</a>";
			}

			$title=$boutique->company_name.' : '.$boutique->tilte;
			$this->load->helper('text');
			$description=word_limiter(str_replace('"',' ',strip_tags($boutique->description)), 250);
			$this->load->helper('string');
			$keywords = preg_split("/[\s,]+/",$boutique->tilte);
			if(is_array($keywords))$keywords = reduce_multiples(implode(',', $keywords),",");
		}else{
			$data=array(
					'title'=>"Cette boutique est désactivée",
					'description'=>"boutique non-trouvable",
					'keywords'=>''
			);
			$data=$setting->data($data);
			$this->view(
					array('header',
							'boutique-desactive',
							'footer'),
							$data); // Display the page
			return false;
		}
		$data=array(
				'title'=>$title,
				'description'=>$description,
				'keywords'=>$keywords,
				'paging'=>self::pagingAnnonce($Total),
				'message'=>$message,
				'search_annonce'=>$search,
				'boutique'=>$boutique,
				'annonces'=>$boutique->annonces);
		$data=$setting->data($data);
		$data['nav_class']['boutique']='active';
		if($Total)
			$this->view(
					array('boutique-header',
							'boutique-info',
							'boutique-show',
							'footer'),
							$data); // Display the page
		else 
			$this->view(
					array('boutique-header',
							'boutique-info',
							'footer'),
							$data); // Display the page
	}
		 
	private function copy_Photo($path,$cat,$photos=array()){
		if(!empty($photos))
		foreach ($photos as $photo){
			$temp_photo=$path.$photo;
			$temp_thumb_photo=$path."thumbnails/".$photo;
			$NameCatPhoto=$cat."/$photo";
	
			if(!is_dir($path.$cat))mkdir($path.$cat, 0777);
			if(!is_dir($path."thumbnails/".$cat))mkdir($path."thumbnails/".$cat, 0777);
	
			if (@copy($temp_photo,"$path/$NameCatPhoto")) unlink($temp_photo);
			if (@copy($temp_thumb_photo,$path."thumbnails/$NameCatPhoto")) unlink($temp_thumb_photo);
	
			$product_photo[]=$NameCatPhoto;
		}
		if(isset($product_photo)) return $product_photo;
	
		return false;
	}
	public function get_info($id,$active=true,$search=''){
		$ID_rechercher=array();
		
		if($search!='') $ID_rechercher=$this->model('annonce_models_chercher')->search_by_words($search);

		if(!empty($ID_rechercher)) $search=$ID_rechercher;
		
		$boutique=$this->model('shop_models_admin')->get_info_shop($id,$search,$this->page,$this->limit);
		//check active
		if($active){
			if(!$boutique)return false;
			if(isset($boutique->shop_active) and !$boutique->shop_active) return false;
		}
		$category=$this->model('category_models_index')->get_category_by_id($boutique->category_id);
		if($category->parent)$boutique->category[]=$this->category_models_index->get_category_by_id($category->parent);
		$boutique->category[]=$category;
		//convertir date
		setlocale(LC_ALL, "fr_BE");
		$date=date('d/m/Y',$boutique->date_create);
		if($date==date('d/m/Y')) $date="Aujourd'hui";
		if($date==date('d/m/Y',strtotime('yesterday'))) $date="Hier";
		$date.=' '.date(' H:i',$boutique->date_create);
		$boutique->date=$date;
		$boutique->annonces=array();
		if(isset($boutique->tous_annonces)){

			$product =$this->load->Module('product/show');
			foreach ($boutique->tous_annonces as $index=>$annonce){
				$annonceInfo=$product->get_info($annonce->prd_id);
				if($annonceInfo)$boutique->annonces[$annonce->prd_id]=$annonceInfo;
				unset($boutique->tous_annonces[$index]);
			}
		}
		return $boutique;
	}
}